package com.androinject.examples.getstarted.activities;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.androinject.android.ManagedActivity;
import com.androinject.examples.getstarted.R;
import com.androinject.examples.getstarted.controller.ExampleController;

public class MyActivity extends ManagedActivity {
	
	/*
	 * Example activity. Please not that this class is derived from the
	 * Andro-Inject class ManagedActivity (which is derived from android.app.Activity).
	 * 
	 * If you want to use Andro-Inject without deriving from ManagedActivity check the Andro-Inject documentation: 
	 * http://code.google.com/p/andro-inject/wiki/documentation#Android_integration_without_Andro-Inject_base_classes
	 */
	
	private ExampleController controller;
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // After calling super.onCreate(..) dependencies have been injected into this activity.
        // The class name 'MyActivity' is automatically expanded to the component id 'myActivity' by Andro-Inject.
        // If you want to use another component id you can call super.onCreate(savedInstanceState, "myComponentId") instead.
        
        this.setContentView(R.layout.main);
    }
    
    
    public void onClick(View view) {
    	// this method is called when the button is pressed.
    	
    	TextView firstView = (TextView)this.findViewById(R.id.firstInput);
    	TextView secondView = (TextView)this.findViewById(R.id.secondInput); 
    	TextView resultView = (TextView)this.findViewById(R.id.result);
    	
    	String first = firstView.getText().toString();
    	String second = secondView.getText().toString();
    	
    	try {
    		// compute the result and show it in resultView.
    		String result = this.controller.computeResult(first, second);
    		String message = this.getString(R.string.result, result);
    		resultView.setText(message);
    		
    	} catch (IllegalArgumentException e) {
    		resultView.setText(this.getString(R.string.error));
		}
    }
    
    
    public void setController(ExampleController controller) {
    	// called by Andro-Inject
    	this.controller = controller;
    }
}
